package cn.xxm.dongbao.user.secure.handler;

import cn.xxm.dongbao.user.secure.access.DosAccessDeniedHandler;
import cn.xxm.dongbao.user.secure.dos.DosStatisticsException;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
public class DosErrorHandler implements DosAccessDeniedHandler {
    String REQUEST_TOO_HIGH = "请求频率太高了,请稍后再试!";

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, DosStatisticsException e) {
        try {
            JSONObject json = new JSONObject();
            json.put("success", false);
            json.put("message", REQUEST_TOO_HIGH);
            String jsonExceptionContent = json.toJSONString();
            httpServletResponse.setContentType("application/json; charset=UTF-8");
            httpServletResponse.getWriter().write(jsonExceptionContent);
            log.info("DosErrorHandler dosStatisticsException");
        } catch (IOException ee) {
            log.error("DosErrorHandler JsonValidatorErrorHandler error");
        }
    }
}